<?php

namespace app\admin\controller\userwork;

use app\admin\controller\Base;
use think\Db;
use think\helper\Time;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Develop extends Base
{

    public function index()
    {

        return view();
    }
    protected $beforeActionList = [
        'first'
    ];

    protected $project_user = 0;

    protected function first()
    {
        $group = DB::name('auth_group_access')->where('uid', session('uid'))->select();
        $group_ids = array_column($group, 'group_id');
        // dump($group_ids);
        //判断当前用户身份
        if (in_array(13, $group_ids) || in_array(1, $group_ids)) {
            //管理员
            $this->project_user = 0;
        } else {
            //开发人员
            $this->project_user = session('uid');
        }
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if ($this->project_user == 0) {
            if (empty($post['uid']) || $post['uid'] == '') {
            } else {
                $map[] = ['admin.id', '=', $post['uid']];
            }
        } else {
            $map[] = ['admin.id', '=', $this->project_user];
        }
        $map[]=['group_id','=',11];
        $data['count'] = Db::view('admin', 'id,nickname')
            ->view('auth_group_access','group_id','admin.id=auth_group_access.uid')
            ->view('user_base','uid,base_wage,work_wage,base_time','admin.id=user_base.uid','LEFT')
            ->where($map)
            ->count();
        $data['data'] =  Db::view('admin', 'id,nickname')
            ->view('auth_group_access','group_id','admin.id=auth_group_access.uid')
            ->view('user_base','uid,base_wage,work_wage,base_time,login_wage,day_wage,leave_wage,sick_wage','admin.id=user_base.uid','LEFT')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        foreach ($data['data'] as &$item){
            $item['month_wage']=$item['base_wage']+($item['login_wage']*4+$item['day_wage'])*21.75+$item['base_time']*$item['work_wage'];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }



    public function editform()
    {
            $post = $this->request->param();
            //单个字段修改
            if (count($post) == 2) {
                $info=DB::name('user_base')->where(['uid' => $post['id']])->find();
                if($info){
                    Db::name('user_base')->where(['uid' => $post['id']])->update($post);
                }else{
                    $post['uid']=$post['id'];
                    unset($post['id']);
                    Db::name('user_base')->insert($post);
                }
                $this->success('操作成功!');
            } else {
                $this->error('提交的信息有误');
            }

    }

}